package com.bdqn.t350.servlet.echarts;

import com.alibaba.fastjson.JSON;
import com.bdqn.t350.entity.LineDataEntity;
import com.bdqn.t350.entity.LineEntity;
import com.bdqn.t350.service.ElectricityService;
import com.bdqn.t350.service.ElectricityServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/echarts/LineServlet")
public class LineServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

//        Integer[] data1 = {120, 132, 101, 134, -90, 230, 210};
//        Integer[] data2 = {20, 232, 11, -144, 90, 30, 90};
//        Integer[] data3 = {180, -12, 161, 84, 60, 230, -10};
//        Integer[] data4 = {280, -22, 161, 84, 60, 230, -10};
//
//        LineDataEntity jd1 = new LineDataEntity();
//        jd1.setName("测试数据1");
//        jd1.setValue(data1);
//
//        LineDataEntity jd2 = new LineDataEntity();
//        jd2.setName("测试数据2");
//        jd2.setValue(data2);
//
//        LineDataEntity jd3 = new LineDataEntity();
//        jd3.setName("测试数据3");
//        jd3.setValue(data3);
//
//        LineDataEntity jd4 = new LineDataEntity();
//        jd4.setName("测试数据4");
//        jd4.setValue(data4);
//
//        List<LineDataEntity> data = new ArrayList<LineDataEntity>();
//        data.add(jd1);
//        data.add(jd2);
//        data.add(jd3);
//        data.add(jd4);
//
//        String[] xcontent = {"测试","b","c","d","e","f","g","h","i"};
//        LineEntity tld = new LineEntity();
//        tld.setXcontent(xcontent);
//        tld.setData(data);

        ElectricityService electricityService = new ElectricityServiceImp();
        //获得横坐标数据，即月份的数据
        List<String> electricityTimeBiz = electricityService.getElectricityTimeBiz();
        //转换成数组
        //Object[] xcontent = electricityTimeBiz.toArray();
        String[] xcontent = electricityTimeBiz.toArray(new String[electricityTimeBiz.size()]);

        //查询需要显示的姓名，即显示几条线
        List<String> byElectricityNameBiz = electricityService.findByElectricityNameBiz();
        List<LineDataEntity> data = new ArrayList<LineDataEntity>();
        for (String s : byElectricityNameBiz) {
            LineDataEntity byElectricityNameCountBiz = electricityService.findByElectricityNameCountBiz(s);
            data.add(byElectricityNameCountBiz);
        }
        LineEntity tld = new LineEntity();
        tld.setXcontent(xcontent);
        tld.setData(data);
        response.getWriter().print(JSON.toJSONString(tld));
    }
}
